// pages/cart/cart.js
import Toast from '../../vant-weapp/toast/toast';
import {
  query,
  post,
  put,
  remove
} from '../../utils/api.js';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pictures: [],//商品集合
    result: [],//选中商品的id集合
    order:[],//订单
    sum:0//总价
  },
  /**
   *购物车商品数量改变
   */
  change(e) {
    let id = e.target.dataset.id;
    let value = e.detail;
    post("updateCart", {
        cartno: id,
        goodsquantity: value
      }),
      query("qureyCart", {
        userno: app.globalData.userno
      }).then(data => {
        let arrys = data.data;
        this.setData({
          //填充数据
          pictures: arrys
        })
      }),
      this.setData({
        sum:0
      });
      let pictures=this.data.pictures;
      this.data.result.forEach(r =>{
       pictures.forEach(p=>{ 
          if(p.goods.goodsno==r){
           let price= p.goods.goodsprice;
           let quantity= p.cart.goodsquantity;
           let sum=price*quantity;
           this.setData({
            sum:this.data.sum+sum
           })
          }
        })
      })
  },
  /**
   * 跳转到商品详情
   */
  toPicture(e) {
    let id = e.target.dataset.id;
    this.setData({
      //填充数据
      id: id
    })
    wx.redirectTo({
      url: '../../pages/picture/picture?goodsno=' + this.data.id
    })
  },
/**
   * 提交订单
   */
  toOrder() {
    if(this.data.result==0){
      Toast('请选择要结算的商品！！！');
    }else{
      let pictures=this.data.pictures;
      this.data.result.forEach(r =>{
       pictures.forEach(p=>{ 
          if(p.goods.goodsno==r){
            this.setData({
              order:this.data.order.concat(p)
            });
          }
        })
      })
      wx.setStorage({
        key:"order",data:this.data.order
      })
      wx.redirectTo({
        url: '../../pages/order/order' 
      })
    }
  },

  //复选框
  checkbox(event) {
    this.setData({
      result: event.detail,
      sum:0
    });
    let pictures=this.data.pictures;
    this.data.result.forEach(r =>{
     pictures.forEach(p=>{ 
        if(p.goods.goodsno==r){
         let price= p.goods.goodsprice;
         let quantity= p.cart.goodsquantity;
         let sum=price*quantity;
         this.setData({
          sum:this.data.sum+sum
         })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    query("qureyCart", {
      userno: 12
    }).then(data => {
      let arrys = data.data;
      this.setData({
        //填充数据
        pictures: arrys
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    query("qureyCart", {
      userno: 12
    }).then(data => {
      let arrys = data.data;
      this.setData({
        //填充数据
        pictures: arrys
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})